package com.jic.market.impl.rest.discountamount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discountamount.model.PromotionAmountModel;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import com.jic.market.mapper.PromotionSingleRuleEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 单一-直降
 * @author yinhaijun
 * @date: 2020/4/7
 */
@Service("SingleFallAmountProcessor")
public class SingleFallAmountProcessor extends AmountProcessor {

    @Autowired
    private PromotionSingleRuleEntityMapper promotionSingleRuleEntityMapper;

    @Override
    protected Boolean isCondition(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, String thirdUserId) {
        if(!checkCommonLimitRule(promotionAmountModel.getPromotionId(), goodsPo.getSkuId(), goodsPo.getNum(), thirdUserId)){
            return false;
        }
        BigDecimal contentValue = promotionSingleRuleEntityMapper.queryContentValueByPromotionIdAndMpId(Long.parseLong(promotionAmountModel.getPromotionId()),goodsPo.getSkuId());
        if(null == contentValue){
            return false;
        }
        promotionAmountModel.setRuleData(contentValue);
        return true;
    }

    @Override
    protected BigDecimal promotionDiscountTotalAmount(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, Object request) {
        // 优惠总额 = 直降*数量
        return Money.multiply((BigDecimal) promotionAmountModel.getRuleData(), new BigDecimal(goodsPo.getNum()));
    }

}
